<template>
	<view class="l_c">
		<view class="bbt">
			<!-- 			<button size="default" open-type="getUserInfo" @getuserinfo="getUserInfoBtn" type="default" class="btn"
				:plain="true">一键登录</button> -->
			<button size="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumberCode" type="default"
				class="btn" :plain="true">一键登录</button>
		</view>
	</view>
</template>

<script>
	import {
		getOpenIdAndSessionKey,
		getUserInfo
	} from '@/common/api.js'
	export default {
		data() {
			return {

			}
		},
		methods: {
			getUserInfoBtn(event) {
				uni.showLoading({
					title: "加载中",
				})
				console.log(event, event.detail.errMsg == 'getUserInfo:ok');
				if (event.detail.errMsg == 'getUserInfo:ok') {

					wx.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							this.setData({
								userInfo: res.userInfo,
								hasUserInfo: true
							})
						}
					})
				}
			},
			getPhoneNumberCode(event) {
				uni.showLoading({
					title: "加载中",
				})
				console.log(event);
				if (event.detail.errMsg == 'getPhoneNumber:ok') {
					function request(url, data) {
						return new Promise((resolve, reject) => {
							uni.request({
								url,
								data,
								method: "POST",
								success: res => {
									resolve(res)
								}
							})
						});
					}
					// 获取微信code
					function getWxCode() {
						return new Promise((resolve, reject) => {
							wx.login({
								success: res => {
									resolve(res.code)
								}
							})
						});
					}
					// 获取sessionKey
					function getsessionKey(code) {
						return new Promise((resolve, reject) => {
							getOpenIdAndSessionKey(code).then(res => {
								console.log(res);
								resolve(res)
							})
						});
					}
					// 解析获取用户信息
					function getUser(data) {
						return new Promise((resolve, reject) => {
							getUserInfo(data).then(res => {
								resolve(res)
							})
						});
					}


					getWxCode().then(res => {
						return getsessionKey(res)
					}).then(res => {
						let data = {
							ivdata: event.detail.iv,
							encrypdata: event.detail.encryptedData,
							sessionkey: res.data.session_key
						}
						return getUser(data)
					}).then(res => {
						console.log('解析后的数据',JSON.parse(res.msg));
						uni.setStorageSync('userInfo',JSON.parse(res.msg))
						uni.hideLoading()
						setTimeout(()=>{
							this.$util.showTost('登录成功')
							uni.reLaunch({
								url:"/pagesHome/home/home?type=3"
							})
						},1000)
				
					})


				} else {
					uni.hideLoading()
					setTimeout(()=>{
						this.$util.showTost('已取消')
					},1000)
				}

			}
		}
	}
</script>

<style lang="scss">
	.l_c {
		width: 100%;
		height: 100vh;
		position: relative;
		background-color: #222;
	}

	image {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
	}

	.bbt {
		padding: 30rpx;
	}

	.btn {
		width: calc(100% - 60rpx);
		position: absolute;
		top: 40%;
		background-color: antiquewhite;
		z-index: 2;
		color: #fff !important;
		border-radius: 20rpx !important;
		border: 1rpx solid #ccc !important;
	}
</style>